function TabInTabBrowser(aTabBrowser)
{
    this.mTabBrowser = aTabBrowser;
}

TabInTabBrowser.prototype = {
    mSelectedItem : null,
    mContextTab : null,

    init : function()
    {
        var b = this.mTabBrowser;

        b.addEventListener('TabOpen', this, true);
        b.addEventListener('TabClose', this, true);
        b.addEventListener('TabMove', this, true);
        b.mTabContainer.addEventListener('select', this, true);
        
        /* initialize menus */
        var self = this;
        this.containerTabMenu = document.getElementById('container-tab-menu');
        document.getElementById('tit-cmd-new-tab').addEventListener('command',
            function(e) {
                self.onAddNewContainerTab();
            }, true);
        document.getElementById('tit-cmd-close-current').addEventListener(
            'command', function(e) {
                BrowserCloseTabOrWindow();
            }, true);
        document.getElementById('tit-cmd-close-all').addEventListener(
            'command', function(e) {
                if(window.confirm('You are about to close multiple tabs. Are you sure you want to continue?')) {
                    if (self.mContextTab) {
                        self.removeAllTabMenuItem(self.mContextTab);
                    }
                }
            }, true);
            
        this.mTabs = document.getElementById('tab-in-tab-list');

        this.addNewContainerTab();

        for (var i = 0; i < b.mTabContainer.itemCount; i++) {
            var tab = b.mTabContainer.getItemAtIndex(i);
            this.initTabMenuItem(tab);
        }

        var tab = this.getTabByTabMenuItem(b.selectedTab.tabMenuItem);
        this.selectTabMenuItem(b.selectedTab.tabMenuItem);

        // Hide the tab bar of tabbrowser
        this.mTabBrowser.setStripVisibilityTo(false);
    },

    initContainerTab : function(aTab)
    {
        if (aTab.mMenupopup) return;
        
        var menupopup = document.createElement('menupopup');
        menupopup.mTab = aTab;
        this.mTabs.parentNode.appendChild(menupopup);
        aTab.mMenupopup = menupopup;
        aTab.mSelectedItem = null;
        var self = this;
        aTab.addEventListener('click', this, true);
        aTab.addEventListener('mousemove', function(e) {
            var tab = self.getTabFromEvent(e);
            self.mContextTab = tab;
        }, true);
        this.mTabs.selectedItem = aTab;
    },

    initTabMenuItem : function(aBrowserTab)
    {
        aBrowserTab.addEventListener('DOMAttrModified', this, true);
        var b = this.mTabBrowser;

        var command = document.createElement('command');
        command.id = this.makeNewId();
        command.setAttribute('label', aBrowserTab.label);
        command.addEventListener('command', function() {
            b.selectedTab = aBrowserTab;
        }, true);
        
        this.mTabs.selectedItem.mMenupopup.appendChild(command);
        aBrowserTab.mCommand = command;
        
        var tabMenuItem = document.createElement('menuitem');
        tabMenuItem.setAttribute('class', 'menuitem-iconic');
        tabMenuItem.setAttribute('command', command.id);
        tabMenuItem.addEventListener('mousemove', this, true);
        tabMenuItem.addEventListener('mouseout', this, true);
        tabMenuItem.originalTab = aBrowserTab;
        aBrowserTab.tabMenuItem = tabMenuItem;
        this.mTabs.selectedItem.mMenupopup.appendChild(tabMenuItem);

        /* determine referring tab */
        var referer = this.mTabs.selectedItem.mSelectedItem;
        if (referer) {
            tabMenuItem.mReferer = referer;
        }

    },
    
    /* this is the method when a tab selected */
    selectTabMenuItem : function(aTabMenuItem)
    {
        var nodes = aTabMenuItem.parentNode.childNodes;
        for (var i = 0; i < nodes.length; i++) {
            var menuitem = nodes[i];
            if (menuitem.nodeType == Node.ELEMENT_NODE &&
                    menuitem.localName == 'menuitem') {
                if (menuitem == aTabMenuItem) {
                    menuitem.setAttribute('checked', 'true');
                } else {
                    menuitem.removeAttribute('checked');
                }
            }
        }
        var tab = this.getTabByTabMenuItem(aTabMenuItem);
        tab.mSelectedItem = aTabMenuItem;
        tab.setAttribute('command', aTabMenuItem.getAttribute('command'));
        this.mTabBrowser.selectedTab = aTabMenuItem.originalTab;
    },
    
    /* this is the method to add a new tab */
    addNewContainerTab : function()
    {
        var newContainerTab = document.createElement('tab');
        newContainerTab.setAttribute('context', 'container-tab-menu');
        newContainerTab.setAttribute('validate', 'never');
        newContainerTab.setAttribute('maxwidth', '250');
        newContainerTab.setAttribute('width', '0');
        newContainerTab.setAttribute('minwidth', '100');
        newContainerTab.setAttribute('flex', '100');
        newContainerTab.setAttribute('class', 'tabbrowser-tab');
        newContainerTab.setAttribute('crop', 'end');
        this.mTabs.appendChild(newContainerTab);
        this.initContainerTab(newContainerTab);
    },
    
    removeAllTabMenuItem : function(aContainerTab)
    {
        var menupopup = aContainerTab.mMenupopup;
        for (var i = 0; i < menupopup.childNodes.length; i++) {
            if (this.mTabBrowser.tabContainer.childNodes.length == 1) {
                closeWindow(true);
                return;
            }
            var menuitem = menupopup.childNodes[i];
            if (menuitem.nodeType == Node.ELEMENT_NODE &&
                    menuitem.localName == 'menuitem') {
                this.mTabBrowser.removeTab(menuitem.originalTab);
                i--;
            }
        }
    },
    
    removeContainerTab : function(aRemoveTab)
    {
        aRemoveTab.parentNode.removeChild(aRemoveTab);
    },

    getTabByTabMenuItem : function(aTabMenuItem)
    {
        return aTabMenuItem.parentNode.mTab;
    },
  
    mPreviewShow : null,

    showPreview: function(aTabMenuItem) {
        var tp = document.getElementById("tabpreview");
        var b = this.mTabBrowser;

        var win = b.getBrowserForTab(aTabMenuItem.originalTab).contentWindow;
        var size = this.getPreviewSize(300, win);
        // draw preview
        this.updatePreview(win, size);
        tp.openPopup(aTabMenuItem, 'start_before', 0, 0, false, false);
        this.mPreviewShow = true;
    },

    hidePreview: function() {
        document.getElementById("tabpreview").hidePopup();
        this.mPreviewShow = false;
    },

    onTabAdded : function(aEvent)
    {
        var tab = aEvent.originalTarget;
        var b = this.mTabBrowser;

        this.initTabMenuItem(tab);

        // Hide the tab bar of tabbrowser
        this.mTabBrowser.setStripVisibilityTo(false);
    },

    onTabRemoved : function(aEvent)
    {
        var tab = aEvent.originalTarget;
        var menuitem = tab.tabMenuItem;
        var menupopup = menuitem.parentNode;
        var command = document.getElementById(
            menuitem.getAttribute('command'));
        menupopup.removeChild(command);
        menupopup.removeChild(menuitem);
        if (menupopup.childNodes.length == 0) {
            var removeTab = menupopup.mTab;
            this.removeContainerTab(removeTab);
        }
    },

    onTabMove : function(aEvent)
    {
        alert('TabMove');
    },

    onTabSelect : function(aEvent)
    {
        var b = this.mTabBrowser;
        var tab = b.selectedTab;
        this.selectTabMenuItem(tab.tabMenuItem);
    },

    onBrowserTabChanged : function(aEvent)
    {
        var tab = aEvent.originalTarget;
        tab.mCommand.setAttribute('label', tab.label);
        tab.mCommand.setAttribute('value', tab.value);
        tab.mCommand.setAttribute('image', tab.image);
    },

    onContainerTabOpenMenu : function(aEvent)
    {
        var tab = this.getTabFromEvent(aEvent);
        menupopup = tab.mMenupopup;
        menupopup.width = tab.boxObject.width;
        menupopup.openPopup(tab, 'after_start', 0, 0, false, false);
    },
    
    onContainerTabSelect : function(aEvent)
    {
        var tab = this.getTabFromEvent(aEvent);
        this.selectTabMenuItem(tab.mSelectedItem);
    },
    
    onAddNewContainerTab : function(aEvent)
    {
        this.addNewContainerTab();
        BrowserOpenTab();
    },

    onMenuItemMouseMove : function(aEvent)
    {
        var menuitem = aEvent.originalTarget;
        if (this.mPreviewShow) return;
        this.showPreview(menuitem);
        if (menuitem.mReferer) {
            menuitem.mReferer.setAttribute('referer', true);
        }
    },

    onMenuItemMouseOut : function(aEvent)
    {
        var menuitem = aEvent.originalTarget;
        this.hidePreview();
        if (menuitem.mReferer) {
            menuitem.mReferer.removeAttribute('referer');
        }
    },

    handleEvent : function(aEvent)
    {
        switch (aEvent.type)
        {
            case 'TabOpen':
                this.onTabAdded(aEvent);
                return;

            case 'TabClose':
                this.onTabRemoved(aEvent);
                return;

            case 'TabMove':
                this.onTabMove(aEvent);
                return;
    
            case 'select':
                this.onTabSelect(aEvent);
                return;

            case 'DOMAttrModified':
                this.onBrowserTabChanged(aEvent);
                return;
            
            case 'click':
                if (aEvent.button == 0) {
                    this.onContainerTabSelect(aEvent);
                    this.onContainerTabOpenMenu(aEvent);
                }
                return;

            case 'mousemove':
                this.onMenuItemMouseMove(aEvent);
                return;

            case 'mouseout':
                this.onMenuItemMouseOut(aEvent);
                return;
        }
    }
};

TabInTabBrowser.prototype.__proto__ = TabInTabService;
